//
//  RecCityViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-23.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecCityViewController.h"
#import "RecCityModel.h"
#import "ListenViewController.h"
#import "RecListenCityTableViewCell.h"
@interface RecCityViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation RecCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor = [UIColor colorWithRed:62/255 green:176/255. blue:193/255. alpha:1];
    [self.view addSubview:view];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100 * WIDTH, 20 * HEIGHT, SCREEN_WIDTH - 200 * WIDTH, 40 * HEIGHT)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = @"选择城市";
    [view addSubview:label];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(20 * WIDTH, 20 * HEIGHT, 50 * WIDTH, 40 * HEIGHT);
    [button addTarget:self action:@selector(goBackController) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
    
    
    
    
    
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStylePlain];
    table.backgroundColor =  [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    table.delegate = self;
    table.dataSource =self;
    [self.view addSubview:table];
    _dataArr = [[NSMutableArray alloc] init];
    [self getData];
}
-(void)goBackController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240 * HEIGHT;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecCityModel *model = [_dataArr objectAtIndex:indexPath.row];
    static NSString *cellID = @"cell";
    RecListenCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RecListenCityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    [cell getData:model];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecCityModel *model = [_dataArr objectAtIndex: indexPath.row];
    [self.delegate giveCity:model];
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)getData
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Property List" ofType:@"plist"];
    NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    for (NSDictionary *dic in [cityDic objectForKey:@"New item"]) {
        RecCityModel *model = [RecCityModel modelWithDictionary:dic];
        [_dataArr addObject:model];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
